from haystack import indexes
from drf_haystack.serializers import HaystackSerializer
from .models import HouseListing


class HouseListingIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=False)
    title = indexes.CharField(model_attr='title')
    address = indexes.CharField(model_attr='address')
    city = indexes.CharField(model_attr='city')
    district = indexes.CharField(model_attr='district')
    description = indexes.CharField(model_attr='description')
    facilities = indexes.CharField(model_attr='facilities')
    price = indexes.DecimalField(model_attr='price')
    area = indexes.DecimalField(model_attr='area')
    house_type = indexes.CharField(model_attr='house_type')
    status = indexes.CharField(model_attr='status')
    room_count = indexes.IntegerField(model_attr='room_count')
    bathroom_count = indexes.IntegerField(model_attr='bathroom_count')
    decoration_type = indexes.CharField(model_attr='decoration_type')
    total_floor = indexes.IntegerField(model_attr='total_floor')
    floor = indexes.IntegerField(model_attr='floor')
    deposit = indexes.DecimalField(model_attr='deposit', null=True)
    orientation = indexes.CharField(model_attr='orientation')
    landlord = indexes.CharField(model_attr='landlord')

    def get_model(self):
        return HouseListing

    def index_queryset(self, using=None):
        return self.get_model().objects.filter(status='available')

    def prepare_text(self, obj):
        return f"{obj.title} {obj.address} {obj.city} {obj.district} {obj.description} {obj.facilities}"

    def prepare_landlord(self, obj):
        return str(obj.landlord)  # 将外键对象转换为字符串








